/**
 *
 **/

layui.define(["jquery", "layer"], function (exports) {
    var model_name = "myDragSort";

    var $ = layui.jquery, layer = layui.layer;

    var obj = {
        options: {
            pelem: '',
            elem: '',
            elemName: '',
            done: []
        }
        , render: function (option) {
            var _this = this;
            _this.options = $.extend(_this.options, option);
            var pelem = $(_this.options.pelem);
            var elem = _this.options.elem;

            $.each(pelem, function () {
                var options = $.extend(true, {elemName: ''}, _this.options);
                if (elem.indexOf(".") == 0) {
                    options.elemName = elem.substr(1);
                } else {
                    console.info("myDragSort config error:elem must className");
                    return false;
                }
                _this.init($(this), options);
            });
            return this;
        }
        , init: function (_pelem, options) {
            var _elem = options.elem;
            var _this = this;
            var draging = null;

            //开始拖动对象
            _pelem.bind('dragstart', function (e) {
                draging = _this.getTarget(e);
                draging = $(draging).closest(_elem, $(this));
            });
            _pelem.bind('dragenter', function (e) {
                _this.preventDefault(e);
            });
            _pelem.bind('dragover', function (e) {
                _this.preventDefault(e);
            });
            //拖动结束
            _pelem.bind('drop', function (e) {
                var target = _this.getTarget(e);
                target = $(target).closest(_elem, $(this));
                _this.preventDefault(e);

                //判断结束的对象是否允许拖动
                if (!target.hasClass(options.elemName)) {
                    return;
                }
                //判断 是否超出了父对象范围
                if (!draging || !$(this).get(0).contains(draging.get(0))) {
                    return;
                }
                //判断拖拽方向，相等表示未拖走
                var targetIndex = $(target).index(), dragingIndex = $(draging).index();
                if (dragingIndex > -1 && targetIndex > -1 && targetIndex != dragingIndex) {
                    if (targetIndex > dragingIndex) { // 向后拖拽
                        var drhtml = $(draging)[0].outerHTML;
                        $(draging).remove();
                        $(target).last().after(drhtml);
                    } else { // 向前拖拽
                        var drhtml = $(draging)[0].outerHTML;
                        $(draging).remove();
                        $(target).first().before(drhtml);
                    }
                    //写回调
                    options.done && typeof options.done === 'function' && options.done(draging, dragingIndex, targetIndex);
                }
            });
        }
        , getTarget: function (event) {
            return event.target || event.srcElement;
        }
        , preventDefault: function (event) {
            if (event.preventDefault) {
                event.preventDefault();
            } else {
                event.returnValue = false;
            }
        }
    }
    exports(model_name, obj);
});